<template>
  <CompOperate
      :field="field"
      :designer="designer"
      :design-state="designState"
      :parent-list="parentList"
      :parent-widget="parentWidget"
      :sub-form-row-id="subFormRowId"
      :sub-form-row-index="subFormRowIndex"
      :sub-form-col-index="subFormColIndex"
      :index-of-parent-list="indexOfParentList"
      :display-style="field.options.displayStyle"
  >
    <div class="scene-DRC-pass-patient-widget" :style="getStyle('viewArea')">
      <div class="widget-inside">
        <div class="DRC-area">
          <div class="dept-room-register-area" :style="getStyle('deptRoom')"
               :class="{'dept-room-register-area2':optionsInfo.checkList.length===1}">
            <div class="dept-info item" v-if="(optionsInfo.checkList).includes(1)"
                 :style="getStyle('deptInfo')">
              <ScrollX> {{ getRoomDeptInfo[optionsInfo['deptNameKey'] || 'deptName'] || '\u00a0' }}</ScrollX>
            </div>
            <div class="room-info item" v-if="(optionsInfo.checkList).includes(3)"
                 :style="getStyle('roomInfo')">
              <ScrollX>{{ getRoomDeptInfo[optionsInfo['roomNameKey'] || 'roomName'] || '\u00a0' }}</ScrollX>
            </div>
          </div>
          <template v-for="patientInfo in getRoomDoctorInfo">
            <div class="patient-area" :style="getStyle('patientInfo')">
              <div class="sign-info item">
                <div class="sign-inside">检查</div>
              </div>
              <template v-for="patientItem in getPatient(patientInfo,'treatingPatient')">
                <div class="patient-info item">
                  <div class="patient-info-inside" :style="getStyle('currentPatientInfo')">
                    {{ patientItem?.patientName || '\u00a0' }}
                  </div>
                </div>
              </template>
            </div>
          </template>

        </div>
        <div class="publish-area">
          <div class="container">
            <template v-if="optionsInfo.publishInfo.tabsName ==='textarea'">
              <div class="text-area">
                <ScrollY :content="optionsInfo.publishInfo.textInfo || ''" :pageDuration="10"/>
              </div>
            </template>
            <template v-else-if="optionsInfo.publishInfo.tabsName ==='video'">
              <div class="video-wrap">
                <div class="video-cover-area" ref="videoInfo" v-if="devType">
                  <i class="el-icon-video-play"></i>
                </div>
                <video class="video-info-area"
                       preload='auto'
                       controls="controls"
                       autoplay
                       @ended="handleEnded"
                       v-if="!devType"
                       :key="widgetKey"
                >
                  <source :src="getSource()" type="video/mp4"/>
                  <source :src="getSource()" type="video/ogg"/>
                  很抱歉 , 当前浏览器不支持现有视频格式 ~
                </video>
              </div>
            </template>
            <template v-else-if="optionsInfo.publishInfo.tabsName ==='picture'">
              <div class="picture-area">
                <img class="logo" :src="getPictureSource()" alt="图片"/>
              </div>
            </template>
          </div>
        </div>
      </div>
    </div>
  </CompOperate>
</template>

<script>
import {v4 as uuid} from "uuid";
import {getQueryString, pxToVw} from '@/utils/common';
import emitter from "@/utils/designer/emitter";
import {buildParentApi} from "@/utils/designer/util";
import ScrollY from '@c/public/seamlessScroll/ScrollY.vue'
import ScrollX from "@c/public/seamlessScroll/ScrollX.vue";
import ScrollEditY from "@c/public/seamlessScroll/ScrollEditY.vue";
import ScrollEditX from "@c/public/seamlessScroll/ScrollEditX.vue";
import mixin from "@sc/templateDetails/modules/homeMiddle/fieldMixin";
import CompOperate from "@c/public/form-designer/devType/modules/compOperate.vue";

let buildParent = buildParentApi();
export default {
  name: "scene-DRC-patient-publish-widget",

  componentName: "FieldWidget", //必须固定为FieldWidget，用于接收父级组件的broadcast事件

  mixins: [emitter, mixin],

  components: {
    ScrollEditY,
    ScrollX,
    ScrollEditX,
    CompOperate,
    ScrollY
  },

  props: {
    widget: Object,
    devType: {
      type: Boolean,
      default: false,
    },
    field: Object,
    parentWidget: Object,
    parentList: Array,
    indexOfParentList: Number,
    designer: Object,
    designState: {
      type: Boolean,
      default: false,
    },
    subFormRowIndex: {
      /* 子表单组件行索引，从0开始计数 */ type: Number,
      default: -1,
    },
    subFormColIndex: {
      /* 子表单组件列索引，从0开始计数 */ type: Number,
      default: -1,
    },
    subFormRowId: {
      /* 子表单组件行Id，唯一id且不可变 */ type: String,
      default: "",
    },
    headTitle: {
      type: String,
      default: "",
    },
    visibleTitle: {
      type: Boolean,
    },
    //prop传入的表单JSON配置
    parentApi: {
      type: Object,
      default: () => buildParentApi()
    },
  },

  data() {
    return {
      number: 0,
      widgetKey: uuid(),
      optionsInfo: Object(),
      pause: require("@a/images/design/pause.png"),
      header: require("@a/images/design/header.jpeg"),
    }
  },

  computed: {
    //生成唯一的字符串
    getOnlyId() {
      return function () {
        return uuid()
      }
    },

    // 设置的行内样式
    getStyle() {
      return function (val) {
        const self = this;
        let info = (self.optionsInfo) || Array();
        let Obj = Object();
        switch (val) {
          case 'viewArea':
            Reflect.set(Obj, 'height', pxToVw(info.headHeight));
            Reflect.set(Obj, 'backgroundColor', `${(info.backgroundColor)}`);
            return Obj;
          case 'deptRoom':
            Reflect.set(Obj, 'backgroundColor', `${(info.deptRoomInfoBackground)}`);
            return Obj;
          case 'deptInfo':
            Reflect.set(Obj, 'color', (info.deptInfoColor));
            Reflect.set(Obj, 'fontWeight', (info.deptInfoFontWeight));
            Reflect.set(Obj, 'fontSize', pxToVw(info.deptInfoFontSize));
            return Obj;
          case 'roomInfo':
            Reflect.set(Obj, 'color', (info.roomInfoColor));
            Reflect.set(Obj, 'fontWeight', (info.roomInfoFontWeight));
            Reflect.set(Obj, 'fontSize', pxToVw(info.roomInfoFontSize));
            return Obj;
          case 'doctorInfo':
            Reflect.set(Obj, 'fontWeight', (info.doctorInfoFontWeight));
            return Obj;
          case 'patientInfo':
            Reflect.set(Obj, 'fontWeight', (info.patientInfoFontWeight));
            return Obj;
          case 'currentPatientInfo':
            Reflect.set(Obj, 'fontSize', pxToVw(info.currentPatientContentFontsize));
            return Obj;
          case 'waitPatientInfo':
            Reflect.set(Obj, 'fontSize', pxToVw(info.waitPatientContentFontsize));
            return Obj;
          case 'tipsSign':
            Reflect.set(Obj, 'color', (info.tipsTitleInfoColor));
            Reflect.set(Obj, 'fontWeight', (info.tipsTitleInfoFontWeight));
            return Obj;
          case 'passPatient':
            Reflect.set(Obj, 'color', (info.passPatientInfoColor));
            Reflect.set(Obj, 'fontWeight', (info.passPatientInfoFontWeight));
            return Obj;
        }
      }
    },

    // 获取科室信息
    getRoomDeptInfo() {
      let devType = (this.devType);
      let roomDoctorInfo = this.$store.getters['getRoomDeptInfo'];
      return devType ? roomDoctorInfo.apiData || Object() : roomDoctorInfo.mockData || Object();
    },

    // 获取全量医患信息
    getRoomDoctorInfo() {
      let devType = (this.devType);
      let roomDoctorInfo = this.$store.getters['getRoomDoctorInfo'];
      let roomInfo = devType ? roomDoctorInfo.apiData : roomDoctorInfo.mockData;
      if (!!(roomInfo?.length)) {
        return roomInfo.slice(0, 1);
      } else {
        return new Array(1).fill({});
      }
    },

    //获取患者
    getPatient() {
      return function (row, key = 'treatingPatient') {
        let list = row[key] || Array();
        switch (key) {
          case 'treatingPatient':
            if (list?.length) {
              return list.slice(0, 1)
            } else {
              return new Array(1).fill({})
            }
          case 'waitingPatientList':
            if (list?.length >= 1) {
              return list.slice(0, 1)
            } else {
              let fillArr = (new Array(1 - list.length).fill({}));
              return [...list, ...fillArr];
            }
        }
      }
    },

    // 获取视频资源
    getSource() {
      return function () {
        let {publishInfo} = this.optionsInfo;
        let video = publishInfo.videoInfo || []
        if (video && video.length) {
          return `${video[this.number].url}?t=${this.getOnlyId()}&info=${video[this.number].url}`;
        }
      }
    },

    // 获取图片资源
    getPictureSource() {
      return function () {
        let {publishInfo} = this.optionsInfo;
        let pictureInfo = publishInfo.pictureInfo || [];
        if (pictureInfo && pictureInfo.length) {
          return `${pictureInfo[0].url}?t=${this.getOnlyId()}&info=${pictureInfo[0].url}`;
        }
      }
    }
  },

  created() {
    this.registerToRefList();
    this.initEventHandler();
    this.handleOnCreated();
  },

  mounted() {
    this.handleOnMounted();
  },

  methods: {
    // 播放结束
    handleEnded(event) {
      let {publishInfo} = this.optionsInfo;
      let videoInfo = event.target.currentSrc;
      let info = getQueryString('info', videoInfo);
      const findIndex = this.$lodash.findIndex((publishInfo.videoInfo), {url: info});
      if (!this.$lodash.isUndefined(findIndex) && !this.$lodash.isNull(findIndex)) {
        let next = (findIndex + 1)
        if (publishInfo.videoInfo.length > next) {
          this.number = next
        } else {
          this.number = 0
        }
      } else {
        this.number = 0
      }
    },

    // 等待候诊滚动结束
    handleScrollEnd() {
      if (self.$refs[`scroll-${row.workerId}`]) {
        console.log('等待就诊滚动结束');
        self.$nextTick(() => {
          self.$refs[`scroll-${row.workerId}`][0].reset()
        })
      }
    },

    // 接收父组件传递过来的数据
    executeMethod(key, data) {
      switch (key) {
        case "roomDeptInfo":
          this.$store.dispatch('setRoomDeptInfo', this.$lodash.cloneDeep(data))
          break;
        case "roomDoctorInfo":
          this.$store.dispatch('setRoomDoctorInfo', this.$lodash.cloneDeep(data))
          break;
          //   关于视频播放
        case "getVideoMedias":
          let element = (this.$refs.videoInfo);
          if (element) {
            let params = Object();
            let info = (this.optionsInfo) || Array();
            const rect = element.getBoundingClientRect();
            const positionInfo = {
              width: Math.ceil(rect.width),
              height: Math.ceil(rect.height),
              top: parseInt(rect.top),
              right: parseInt(rect.right),
              bottom: parseInt(rect.bottom),
              left: parseInt(rect.left),
            };
            Reflect.set(params, 'positionInfo', positionInfo);
            Reflect.set(params, 'video', info['publishInfo']['videoInfo']);
            this.parentApi.executeParentMethod('playVideoMedias', params);
          }
          break;
      }
    },
  },

  watch: {
    field: {
      handler: function (val) {
        this.widgetKey = uuid();
        let key = val.options.config;
        this.optionsInfo = val.options[key];
      },
      deep: true,
      immediate: true
    }
  },

  destroyed() {
    this.unregisterFromRefList();
  },

};
</script>

<style lang="scss" scoped>
.scene-DRC-pass-patient-widget {
  color: #333;
  font-size: 40px;
  width: 100%;
  background: transparent;
  box-sizing: border-box;
  overflow: hidden;

  .widget-inside {
    height: calc(100%);
    width: 100%;
    border-radius: 18px;
    padding: 34px 30px 34px 30px;
    box-sizing: border-box;
    overflow: hidden;

    .DRC-area {
      margin-bottom: 30px;
      box-sizing: border-box;
      width: 100%;
      height: calc(100% - 574px - 30px);

      .dept-room-register-area {
        width: 100%;
        margin-bottom: 30px;
        background: #0059B3;
        border-radius: 20px;
        overflow: hidden;
        padding: 70px 90px 90px 90px;
        box-sizing: border-box;
        height: calc(100% - 250px - 30px);
        display: flex;
        flex-direction: column;

        .item {
          height: calc(100% / 2);
          font-size: 160px;
          align-items: center;
          align-content: center;
          justify-content: center;
          color: #FFFFFF;
          white-space: nowrap;
        }

        .room-info {
          border-radius: 18px;
          overflow: hidden;
          color: #0059B3;
          padding: 0 28px;
          background: white;
        }
      }

      .dept-room-register-area2 {
        .item {
          height: calc(100% / 1);
        }
      }

      .patient-area {
        width: 100%;
        background: rgba(0, 89, 179, 0.2);
        border-radius: 20px;
        overflow: hidden;
        height: 250px;
        display: flex;
        align-items: center;
        flex-direction: row;

        .item {
          height: 100%;
          flex-shrink: 0;
          flex-basis: 0;
          overflow: hidden;
          align-content: center;
        }

        .sign-info {
          font-size: 60px;
          flex-basis: 124px;
          color: white;
          display: flex;
          align-items: center;
          align-self: center;
          align-content: center;
          justify-content: center;
          background: rgba(0, 89, 179, 1);

          .sign-inside {
            width: 90px;
            text-align: center;
            color: #FFFFFF;
            line-height: 1.46;
            align-items: center;
          }
        }

        .patient-info {
          flex-grow: 1;
          box-sizing: border-box;
          color: rgba(0, 89, 179, 1);
          display: flex;
          align-content: center;
          align-self: center;
          align-items: center;
          justify-content: center;

          .patient-info-inside {
            overflow: hidden;
            width: 100%;
            font-size: 134px;
            text-align: center;
            @include singleEllipsis(100%);
          }

        }
      }
    }

    .publish-area {
      height: 574px;
      width: 100%;
      background: white;
      border-radius: 20px;
      overflow: hidden;

      :deep(.container) {
        width: 100%;
        height: 100%;
        box-sizing: border-box;

        .text-area {
          padding: 34px 28px;
          height: 100%;
          width: 100%;
          box-sizing: border-box;
          overflow: hidden;
        }

        .video-wrap {
          height: 100%;
          width: 100%;
          overflow: hidden;
          position: relative;

          .video-cover-area {
            width: 100%;
            height: 100%;
            min-width: 100%;
            overflow: hidden;
            border-radius: 8px;
            display: flex;
            justify-content: center;
            align-items: center;
            background: rgba(0, 0, 0, 0.8);

            i {
              font-size: 150px;
              color: #EBEEF5;
            }
          }

          .video-info-area {
            position: absolute;
            inset: 0;
            overflow: hidden;
            border-radius: 8px;
            width: 100%;
            height: 100%;
          }
        }

        .picture-area {
          width: 100%;
          height: 100%;
          overflow: hidden;

          img {
            width: 100%;
            height: 100%;
            object-fit: contain;
          }
        }
      }
    }
  }
}
</style>

